package com.yyj.web.controller.manager;

import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.enums.PaymentStatusEnum;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.service.ManagerService;
import com.yyj.service.PaymentService;
import com.yyj.service.WaterBillService;
import com.yyj.service.WeixinPayService;
import com.yyj.util.DateUtil;
import com.yyj.util.RspUtils;
import com.yyj.web.controller.BaseController;
import com.yyj.web.protocol.PageRspData;
import com.yyj.web.protocol.StoreResponse;
import com.yyj.web.protocol.req.*;
import com.yyj.web.protocol.resp.BackendPaymentListResp;
import com.yyj.web.protocol.resp.BackendWaterBillListResp;
import com.yyj.web.protocol.resp.PaymentQueryResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "后台相关接口" , description="后台相关接口")

@RestController
public class BackendController extends BaseController {

    @Autowired
    PaymentService paymentService;

    @Autowired
    WaterBillService waterBillService;

    @Autowired
    ManagerService managerService;

    @ApiOperation(value = "后台-查询业主费用记录列表")
    @RequestMapping(value = "/backend/payments/list", method = RequestMethod.GET)
    public Object backendPaymentList(BackendPaymentListReq request
    ){
        try{
            request = null == request ? new BackendPaymentListReq(): request;
            request.setCurrentPage(request.getPageIndex()+1);
            request.setPageSize(request.getLimit());
            request.setStatus(PaymentStatusEnum.IS_PAY.getCode());
            PageRspData<BackendPaymentListResp> pageRspData = paymentService.findBackendPaymentList(request);
            StoreResponse rsp = new StoreResponse();
            rsp.setRows(pageRspData.getData());
            rsp.setMsg("success");
            rsp.setResults(pageRspData.getTotal().intValue());
            rsp.setHasError(false);
            rsp.setRc(ReturnCode.NORMAL_SUCCESS);
            return rsp;
        }catch(Exception e){
            logger.error("后台-查询业主费用记录列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "后台-查询业主水表记录列表")
    @RequestMapping(value = "/backend/waterBills/list", method = RequestMethod.GET)
    public Object backendWaterFeeList(BackendWaterBillListReq request
    ){
        try{
            request = null == request ? new BackendWaterBillListReq(): request;
            request.setCurrentPage(request.getPageIndex()+1);
            request.setPageSize(request.getLimit());
            PageRspData<BackendWaterBillListResp> pageRspData = waterBillService.queryBackendWaterBillList(request);
            StoreResponse rsp = new StoreResponse();
            rsp.setRows(pageRspData.getData());
            rsp.setMsg("success");
            rsp.setResults(pageRspData.getTotal().intValue());
            rsp.setHasError(false);
            rsp.setRc(ReturnCode.NORMAL_SUCCESS);
            return rsp;
        }catch(Exception e){
            logger.error("后台-查询业主费用记录列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "后台-编辑业主水表记录")
    @RequestMapping(value = "/backend/waterBills/edit/{id}", method = RequestMethod.GET)
    public Object backendEditWaterFee(@PathVariable Long id
    ){
        try{
            return RspUtils.success(waterBillService.queryWaterBillDetail(id));
        }catch(Exception e){
            logger.error("后台-编辑业主水表记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "后台-修改业主水表记录")
    @RequestMapping(value = "/backend/waterBills/update", method = RequestMethod.POST)
    public Object backendUpdateWaterFee(@RequestBody @Validated BackendWaterBillUpdateReq request , BindingResult result
    ){
        try{
            checkRequestParams(result);
            WaterBillManagerUpdateReq waterBillManagerUpdateReq = new WaterBillManagerUpdateReq();
            waterBillManagerUpdateReq.setCurrentId(request.getId());
            waterBillManagerUpdateReq.setReadNum(request.getReadNum());
            return RspUtils.success(waterBillService.managerUpdateWaterBill(waterBillManagerUpdateReq));
        }catch(Exception e){
            logger.error("后台-修改业主水表记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "后台-用户登录")
    @RequestMapping(value = "/backend/login", method = RequestMethod.POST)
    public BaseResponse login(@RequestBody @Validated ManagerLoginReq request , BindingResult result
    ){
        try{
            checkRequestParams(result);
            return managerService.login(request);
        }catch(Exception e){
            logger.error("后台-登录异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "后台-用户退出登录")
    @RequestMapping(value = "/backend/logout", method = RequestMethod.POST)
    public BaseResponse logout(
    ){
        try{
            return RspUtils.success();
        }catch(Exception e){
            logger.error("后台-登录异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }
}


